package com.sina.shop.api;

import com.sina.shop.domain.order.Order;
import com.sina.shop.domain.order.Status;
import com.sina.shop.service.order.OrderService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-08-11:32
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    @Qualifier("orderService")
    private OrderService orderService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getOrderListByAdminId", method = RequestMethod.GET)
    public List<Order> getOrderListByAdminId(Long adminId, Status status) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getOrderListByAdminId, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return orderService.getOrderListByAdminId(adminId, status);
    }

    @RequestMapping(value = "/getOrder", method = RequestMethod.POST)
    public Order getOrder(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getOrder, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return orderService.getOrder(id);
    }

    @RequestMapping(value = "/deleteOrder", method = RequestMethod.POST)
    public Integer deleteOrder(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteOrder, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return orderService.deleteOrder(id);
    }

    @RequestMapping(value = "/updateOrderStatus", method = RequestMethod.POST)
    public Integer updateOrderStatus(Long adminId, Status status) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateOrderStatus, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return orderService.updateOrderStatus(adminId, status);
    }

    @RequestMapping(value = "/saveOrder", method = RequestMethod.POST)
    public Order saveOrder(Order order) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveOrder, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return orderService.saveOrder(order);
    }

    @RequestMapping(value = "/updateOrder", method = RequestMethod.POST)
    public Order updateOrder(Order order) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateOrder, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return orderService.updateOrder(order);
    }
}
